using System;
using Impact.Model.Objects;
using System.Web.UI.WebControls;
using Impact.Web.Foundation.Controls;

namespace Impact.Web.Admin
{
    public partial class EditEmailTemplate : Impact.Web.Foundation.PageBase
    {

        /// <summary>
        /// perform data binding specific to this page
        /// </summary>
        protected override void Bind()
        {
            base.Bind();

            templates.DataSource = new EmailTemplateCollection().Load();
            templates.DataTextField = EmailTemplate.Columns.Name;
            templates.DataValueField = EmailTemplate.Columns.TemplateID;
            templates.DataBind();

        }

        /// <summary>
        /// event handler executed when the user selects a template
        /// </summary>
        protected void TemplateSelected( object sender, EventArgs e )
        {
            EmailTemplate template = EmailTemplate.FetchByID( templates.SelectedValue );
            if ( template != null && template.TemplateID > 0 )
            {
                subject.Text = template.Subject;
                templateTextBox.Text = template.Body;
            }

        }

        /// <summary>
        /// event handler executed when the user clicks Save
        /// </summary>
        protected void SaveTemplate( object sender, EventArgs e )
        {
            EmailTemplate template = EmailTemplate.FetchByID( templates.SelectedValue );
            if ( template != null && template.TemplateID > 0 )
            {
                template.Subject = subject.Text;
                template.Body = templateTextBox.Text;
                template.Save();
                MessageBox.Show( "Template Saved" );
            }
        }

        /// <summary>
        /// Event handler fired when the user clicks cancel
        /// </summary>
        protected void CancelCommand( object sender, EventArgs e )
        {
            Return();
        }


    }
}
